<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="wcs_device_type" placeholder="请选择设备类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="货位类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeType">
              <j-dict-select-tag type="list" v-model="model.nodeType" dictCode="wcs_node_type" placeholder="请选择货位类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coord">
              <a-input v-model="model.coord" placeholder="请输入坐标"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="X" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="x">
              <a-input-number v-model="model.x" placeholder="请输入X"  style="width: 100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="Y" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="y">
              <a-input-number v-model="model.y" placeholder="请输入Y"  style="width: 100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="Z" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="z">
              <a-input-number v-model="model.z" placeholder="请输入Z"  style="width: 100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="地码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groundCode">
              <a-input v-model="model.groundCode" placeholder="请输入地码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否有货" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="nodeStatus">
              <j-dict-select-tag type="list" v-model="model.nodeStatus" dictCode="wcs_have_cargo" placeholder="请选择是否有货" />
            </a-form-model-item>
          </a-col>
          <a-divider/>
          <a-col :span="6">
            <a-form-model-item label="可向上" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="beUp">
              <a-checkbox v-model:checked="model.beUp"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="可向下" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="beDown">
              <a-checkbox v-model:checked="model.beDown"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="可向左" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="beLeft">
              <a-checkbox v-model:checked="model.beLeft"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="可向右" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="beRight">
              <a-checkbox v-model:checked="model.beRight"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" v-if="model.type == 4">
            <a-form-model-item label="巷道号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roadNum">
              <a-input-number v-model="model.roadNum" placeholder="请输入巷道号(堆垛机用)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="x轴偏移量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="offsetX">
              <a-input-number v-model="model.offsetX" placeholder="请输入x轴偏移量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="y轴偏移量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="offsetY">
              <a-input-number v-model="model.offsetY" placeholder="请输入y轴偏移量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WcsMapInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        labelCol1: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        wrapperCol1: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        confirmLoading: false,
        validatorRules: {
          coord: [
            {required: true, message: '请输入编码!'},
            {validator: (rule, value, callback) => validateDuplicateValue('wcs_map_info', 'coord', value, this.model.id, callback)}
          ],
          nodeType: [
            {required: true, message: '请输入货位类型!'},
          ],
        },
        url: {
          add: "/wcs/wcsMapInfo/add",
          edit: "/wcs/wcsMapInfo/edit",
          queryById: "/wcs/wcsMapInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>